package com.monitor.action.login;

import com.monitor.action.MonitorActionSupport;
import com.monitor.po.Administrator;
import com.monitor.service.AdminService;
import com.monitor.service.UserService;
import com.monitor.util.Constants;

/**
 * @author zzy
 * @catagory 后台管理员登陆
 */
public class LoginAction extends MonitorActionSupport {

	/**
	 * 序列号
	 */
	private static final long serialVersionUID = -1610546509688140094L;
	// 用户名
	private String username;
	// 密码
	private String password;
	private AdminService adminService;
	private UserService userService;

	/**
	 * 管理员登陆
	 * 
	 * @return
	 * @throws Exception
	 */
	public String execute() throws Exception {
		if (isLogined()) {
			// 如果已经登录，直接跳转到主页面
			return SUCCESS;
		} else {
			// 没有登录
			Administrator admin = adminService.login(username, password);
			if (admin != null) {
				session.put(Constants.SESSION_USER, admin);
				session.put(Constants.SESSION_USERNAME, admin.getUsername());
				return SUCCESS;
			} else {
				addFieldError("username", getText("username.password.error"));
				return INPUT;
			}
		}
	}

	public void shutdown() throws Exception {

	}

	/**
	 * 注销
	 * 
	 * @return
	 * @throws Exception
	 */
	public String logout() throws Exception {
		session.clear();
		return SUCCESS;
	}

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username
	 *            the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the adminService
	 */
	public AdminService getAdminService() {
		return adminService;
	}

	/**
	 * @param adminService
	 *            the adminService to set
	 */
	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
